package com.datamation.swadeshi.control.data;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.datamation.swadeshi.model.Control;
import com.datamation.swadeshi.model.Dealer;
import com.datamation.swadeshi.model.LoadingPrintPre;
import com.datamation.swadeshi.model.SalOrdPrintPre;

public class LoadingPrintDS {
	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	private String TAG="swadeshi";
	
	
	public LoadingPrintDS (Context context){
		this.context = context;
		dbHelper = new DatabaseHelper(context);
	}
	
	public void open() throws SQLException {
		dB = dbHelper.getWritableDatabase();
	}
	
	
	
	
	public ArrayList<LoadingPrintPre> getAllLoadingListPreview(String currentRefNo) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<LoadingPrintPre> list = new ArrayList<LoadingPrintPre>();
		//String currentRefNo = "EXASA/000010";
		String selectQuery = "SELECT LH.RefNo,LD.CaseQty,LD.PiceQty,LD.Amt,FI.Itemname,LD.SeqNo,LH.Locfrom,LH.Locto,LH.TxnDate,LH.Txntype FROM fTranHed LH,ftranDet LD, Fitem as FI WHERE LD.RefNo = LH.RefNo AND FI.ItemCode = LD.ItemCode and  "
				+ "LH.RefNo = '"+ currentRefNo  + "'  order by LD.SeqNo ASC" ;
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		while(cursor.moveToNext()){
			
			LoadingPrintPre aLoadPrintPre=new LoadingPrintPre();
			
			aLoadPrintPre.setLOAD_PRINT_REF_NO(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_REFNO)));
			aLoadPrintPre.setLOAD_PRINT_ITEMNAME(cursor.getString(cursor.getColumnIndex(dbHelper.FITEM_ITEM_NAME)));
			aLoadPrintPre.setLOAD_PRINT_CASE_QTY(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANDET_CASEQTY)));
			aLoadPrintPre.setLOAD_PRINT_PIECE_QTY(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANDET_PICEQTY)));
			aLoadPrintPre.setLOAD_PRINT_AMT(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANDET_AMT)));
			aLoadPrintPre.setLOAD_PRINT_SEQNO(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANDET_SEQNO)));
			aLoadPrintPre.setLOAD_PRINT_LOCFROM(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_LOCFROM)));
			aLoadPrintPre.setLOAD_PRINT_LOCTO(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_LOCTO)));
			aLoadPrintPre.setLOAD_PRINT_DATE(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_TXNDATE)));
			aLoadPrintPre.setLOAD_TXN_TYPE(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANHED_TXNTYPE)));
			list.add(aLoadPrintPre);
			System.out.println(list.size());
			
		}
		
		return list;
	}
	
	
	public String getTotalCaseQtyReturns(String refno) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}

		//String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FREASON +" WHERE "+dbHelper.FREASON_NAME+"='"+name+"'";
		@SuppressWarnings("static-access")
		String selectQuery = "select sum(FD.CaseQty) as CaseQtySum from ftranDet FD where refno = '"+ refno  +"'";
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){
			if (cursor.getString(cursor.getColumnIndex("CaseQtySum"))!=null)
				return cursor.getString(cursor.getColumnIndex("CaseQtySum"));
			else
				return "0";
			
		}
		
		return "0";
	}
	
	
	public String getTotalPieceQtyReturns(String refno) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}

		//String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FREASON +" WHERE "+dbHelper.FREASON_NAME+"='"+name+"'";
		@SuppressWarnings("static-access")
		String selectQuery = "select sum(FD.PiceQty) as PieceQtySum from ftranDet FD where refno ='"+ refno  +"'";
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){
			if (cursor.getString(cursor.getColumnIndex("PieceQtySum"))!=null)
				return cursor.getString(cursor.getColumnIndex("PieceQtySum"));
			else
				return "0";
			
		}
		
		return "0";
	}
	
	public String getTotalAmountReturns(String refno) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}

		//String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FREASON +" WHERE "+dbHelper.FREASON_NAME+"='"+name+"'";
		@SuppressWarnings("static-access")
		String selectQuery = "select sum(FD.Amt) as totalAmtSum from ftranDet FD where refno ='"+ refno  +"'";
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){
			if (cursor.getString(cursor.getColumnIndex("totalAmtSum"))!=null)
				return cursor.getString(cursor.getColumnIndex("totalAmtSum"));
			else
				return "0";
			
		}
		
		return "0";
	}
	
	
	
	
}
